<?php

class HTMLElement extends HTMLDOM {
	var $inlineTags = array("a", "span", "img");
	var $endedTags = array("td", "li", "script", "textarea", "iframe", "frame", "div", "a");
	var $startLine;
	var $endLine;
	var $nodeStyle = "block";
	var $nodeName;
	var $nodeType = DOM_ELEMENT_NODE;
	var $code;
	
	function HTMLElement($nodeName, $document) {
		$this->nodeName = strtolower($nodeName);
		$this->document = $document;
		$this->nodeStyle = in_array($this->nodeName, $this->inlineTags) ? 'inline' : 'block';
	}
	
	function toString() {
		$string = "<$this->nodeName";
		
		foreach ($this->attributes as $name => $value)
			$string .= " $name=\"$value\"";
		
		if (isset($this->code))
			$string .= $this->code;
		
		if (count($this->childNodes) == 0 && !in_array($this->nodeName, $this->endedTags)) {
			$string .= " />";
		} else {
			$string .= ">";
		
			$tabs = $this->getTabs();
			$childTabs = $tabs . ($this->nodeName == 'html' ? '' : $this->tab);
			
			$isOnlyInline = true;
			$inline = $this->inlineTags;
			foreach ($this->childNodes as $node) {
				
				$first = isset($first) ? false : true;
				if ($this->noWhite ||
						$node->nodeStyle == 'inline' ||
						($first && ($this->nodeName == 'td' || $this->nodeName == 'li'))) {
					$string .= $node->toString();
				} elseif ($node->nodeType == DOM_PHP_NODE) {
					$string .= "\n" . $node->toString();
				} else {
					$isOnlyInline = false;
					$string .= "\n$childTabs" . $node->toString();
				}
			}
			
			if ($isOnlyInline || in_array($this->nodeName, $inline) ||
					$this->nodeName == "td" || $this->nodeName == "li") {
				$string .= "</$this->nodeName>";
			} else {
				$string .= "\n$tabs</$this->nodeName>";
			}
		}
		return $string;
	}
	
	// private function
	function getTabs() {
		$parent = $this->parentNode;
		$tabs = "";
		
		while(strtolower(get_class($parent)) != 'htmldocument'
				&& $parent->nodeName != 'html'
				&& $parent->nodeName != 'head'
				&& $parent->nodeName != 'body') {
			$tabs .= $this->tab;
			$parent = $parent->parentNode;
		}
		return $tabs;
	}
	
}

?>